package puzzle.nurikabe.solver;

import puzzle.common.data.HatanException;
import puzzle.common.point.Direction;
import puzzle.common.solver.BoardRule;
import puzzle.nurikabe.data.NurikabeBoard;
import puzzle.nurikabe.data.StateUtil;

/**
 * 数字の入ったシマ同士は壁で隔てられている、というルール。
 */
class ShimaIsolationRule extends NurikabeCellRule implements BoardRule<NurikabeBoard> {

	@Override
	protected boolean apply(NurikabeBoard b, int x, int y) throws HatanException {
		if (b.getBoardState(x, y) != 0) {
			return false;
		}

		boolean isChanged = false;
		int n0 = 0;

		for (Direction d : Direction.CLOCKWISE4) {
			int n = StateUtil.decodeOwningShima(b.getBoardState(x + d.v.dx, y + d.v.dy));

			if (n != 0) {
				if (n0 == 0) {
					n0 = n;
				} else if (n0 != n) {
					isChanged |= b.setKabe(x, y);
					break;
				}
			}
		}
		return isChanged;
	}

}
